<?php


namespace app\libs\exception;


class ExceptionCode
{
    private $exception_message = [
        0 => "success",
        99999 => "服务器未知异常",

        10000 => "通用错误",
        10001 => "通用参数错误",
        10002 => "没有找到合适的登录处理方法",
        10003 => "令牌不合法或者过期",
        10004 => "参数必须是数字",
        10005 => "指定异常代码不存在",

        20000 => "用户类通用异常",
        20001 => "用户已存在",
        20002 => "用户不存在",
        20003 => "密码错误",
        20004 => "您没有权限访问",

        30000 => "资源错误"
    ];

    /**
     * @throws ParamException
     */
    public function judgeCode($statusCode): bool
    {
        if (!is_numeric($statusCode)) {
            throw new ParamException(10005);
        }

        if (array_key_exists($statusCode, $this->exception_message)) {
            return true;
        } else {
            throw new ParamException(10005);
        }
    }

    /**
     * @throws ParamException
     */
    public function issueMessage($statusCode): string
    {
        $trial_result = $this->judgeCode($statusCode);

        if ($trial_result) {
            return $this->exception_message[$statusCode];
        }

        return $this->exception_message[99999];
    }
}